/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadSubSetor.java
 *
 * Created on 21/11/2011, 13:57:54
 */
package visao;

import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import setor.SetorNaoCadastradoException;
import subSetor.SubSetorNaoCadastradoException;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;
import util.Util;
import fachada.FachadaBDR;
import java.awt.Color;
import javax.swing.JOptionPane;
import setor.Setor;
import subSetor.SubSetor;

/**
 *
 * @author Administrador
 */
public class CadSubSetor extends javax.swing.JFrame {

    /** Creates new form CadSubSetor */
    public CadSubSetor() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/visao.gif")).getImage());

        // Setando os valores para zero e desabilitando os campos
        jTextField_QuantArmazenada.setEnabled(false);
        jCheckBox_Status.setSelected(true);
    }

    /** Creates new form CadSubSetor */
    public CadSubSetor(int idSubSetor) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/visao.gif")).getImage());
        
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // obtendo os Objetos da base
            SubSetor objSubSetor = objFachadaBDR.procurarSubSetor(idSubSetor);
            Setor objSetor = objSubSetor.getSetor();

            // Setando a subSetor
            jComboBox_idSetor.setSelectedItem(new KeyValue(objSetor.getDescricaoSetor(), Integer.toString(objSetor.getIdSetor())));
            id_sub_setor.setText(String.valueOf(objSubSetor.getIdSubSetor()));
            jTextField_DescricaoSetor.setText(objSubSetor.getDescricaoSubSetor());
            jTextField_QuantArmazenada.setText(String.valueOf(objSubSetor.getQuantidadeArmazenada()));
            jTextField_QuantMaxima.setText(String.valueOf(objSubSetor.getQuantidadeMaxima()));
            jTextField_QuantMinina.setText(String.valueOf(objSubSetor.getQuantidadeMinima()));
            jCheckBox_Status.setSelected(objSubSetor.getStatusSubSetor());


        } catch (SubSetorNaoCadastradoException ex) {
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        } catch (CamposObrigatoriosException ex) {

            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        } catch (ErroAcessoRepositorioException ex) {

            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jCheckBox_Status = new javax.swing.JCheckBox();
        jLabel_idSetor = new javax.swing.JLabel();
        jTextField_QuantMaxima = new javax.swing.JTextField();
        jTextField_QuantMinina = new javax.swing.JTextField();
        jTextField_QuantArmazenada = new javax.swing.JTextField();
        jLabel_QuantMinima = new javax.swing.JLabel();
        jTextField_DescricaoSetor = new javax.swing.JTextField();
        jLabel_Nome = new javax.swing.JLabel();
        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_QuantMaxima1 = new javax.swing.JLabel();
        jLabel_QuantArmazenada1 = new javax.swing.JLabel();
        jComboBox_idSetor = new javax.swing.JComboBox();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Mensagem = new javax.swing.JLabel();
        id_sub_setor = new javax.swing.JTextField();

        jTextField1.setText("jTextField1");

        setTitle("Cadastro SubSetor");
        setResizable(false);

        jCheckBox_Status.setSelected(true);
        jCheckBox_Status.setText("Ativo");

        jLabel_idSetor.setText("Setor :");

        jTextField_QuantMaxima.setText("0");

        jTextField_QuantMinina.setText("0");

        jTextField_QuantArmazenada.setText("0");
        jTextField_QuantArmazenada.setEnabled(false);

        jLabel_QuantMinima.setText("Quantidade Minima :");

        jLabel_Nome.setText("Nome :");

        jLabel_Codigo.setText("Codigo :");

        jLabel_QuantMaxima1.setText("Quantidade Maxima :");

        jLabel_QuantArmazenada1.setText("Quantidade Armazenada :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            List<Setor> listSetor = objFachadaBDR.listarSetor("");

            jComboBox_idSetor.addItem(new KeyValue("--- Favor Selecionar ---","0"));
            for (Setor objSetor : listSetor) {
                jComboBox_idSetor.addItem(new KeyValue(objSetor.getDescricaoSetor(), Integer.toString(objSetor.getIdSetor())));
            }

        } catch (Exception e) {
        }

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        id_sub_setor.setText("0");
        id_sub_setor.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_QuantMaxima1)
                            .addComponent(jLabel_QuantArmazenada1)
                            .addComponent(jLabel_QuantMinima))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField_QuantArmazenada, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField_QuantMaxima, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                                .addComponent(jTextField_QuantMinina))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(id_sub_setor, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox_idSetor, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel_Nome)
                        .addComponent(jLabel_idSetor)
                        .addComponent(jLabel_Codigo))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTextField_DescricaoSetor, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jCheckBox_Status)
                            .addGap(41, 41, 41)))
                    .addContainerGap(54, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(id_sub_setor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54)
                .addComponent(jComboBox_idSetor, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_QuantArmazenada1)
                    .addComponent(jTextField_QuantArmazenada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel_QuantMaxima1))
                    .addComponent(jTextField_QuantMaxima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_QuantMinina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_QuantMinima))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_Cancelar)
                    .addComponent(jLabel_Mensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Ok))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jCheckBox_Status)
                        .addComponent(jLabel_Codigo))
                    .addGap(21, 21, 21)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField_DescricaoSetor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel_Nome))
                    .addGap(27, 27, 27)
                    .addComponent(jLabel_idSetor)
                    .addContainerGap(181, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:


        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {

            dispose();

        } else {
        }


    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:

        int idSubSetor = Integer.parseInt(id_sub_setor.getText());
        String nome = jTextField_DescricaoSetor.getText();
        int idSetor = Integer.parseInt(((KeyValue) jComboBox_idSetor.getSelectedItem()).getValue());
        int valorArmazenado = Integer.parseInt(jTextField_QuantArmazenada.getText());
        int valorMaximo = Integer.parseInt(jTextField_QuantMaxima.getText());
        int valorMinimo = Integer.parseInt(jTextField_QuantMinina.getText());
        boolean status = jCheckBox_Status.isSelected();

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Setor setor = objFachadaBDR.procurarSetor(idSetor);
            SubSetor subSetor = new SubSetor(idSubSetor, setor, nome, valorMaximo, valorMinimo, valorArmazenado, status);

            if (idSubSetor == 0) {
                objFachadaBDR.cadastrarSubSetor(subSetor);
            } else {
                objFachadaBDR.atualizarSubSetor(subSetor);
            }

            String acao = (idSubSetor == 0) ? "Cadastrado" : "Atualizado";
            jLabel_Mensagem.setText("SubSetor " + acao + " com Sucesso");
            jLabel_Mensagem.setForeground(Color.BLACK);

            if (idSubSetor == 0) {
                jTextField_DescricaoSetor.setText("");
                jTextField_QuantArmazenada.setText("");
                jTextField_QuantMaxima.setText("");
                jTextField_QuantMinina.setText("");
                jCheckBox_Status.setSelected(false);
                jComboBox_idSetor.setSelectedIndex(0);
            }

        } catch (SetorNaoCadastradoException ex) {

            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        } catch (CamposObrigatoriosException ex) {

            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);

        } catch (ErroAcessoRepositorioException ex) {

            ex.printStackTrace();
            jLabel_Mensagem.setText(ex.getMessage());
            jLabel_Mensagem.setForeground(Color.red);
        }


    }//GEN-LAST:event_jButton_OkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadSubSetor().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField id_sub_setor;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JCheckBox jCheckBox_Status;
    private javax.swing.JComboBox jComboBox_idSetor;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Mensagem;
    private javax.swing.JLabel jLabel_Nome;
    private javax.swing.JLabel jLabel_QuantArmazenada1;
    private javax.swing.JLabel jLabel_QuantMaxima1;
    private javax.swing.JLabel jLabel_QuantMinima;
    private javax.swing.JLabel jLabel_idSetor;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField_DescricaoSetor;
    private javax.swing.JTextField jTextField_QuantArmazenada;
    private javax.swing.JTextField jTextField_QuantMaxima;
    private javax.swing.JTextField jTextField_QuantMinina;
    // End of variables declaration//GEN-END:variables
}
